package meidi;

import java.util.Scanner;

/**
 * @author Hang
 * @date 2022/10/10
 * @project AutumnOffer
 * @description
 **/
public class solution3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        String s = sc.next();
        String t = sc.next();
        int max = 0;
        for(int i=0;i<n;i++){
            int count = 0;
            for(int j=0;j<m;j++){
                if(s.charAt(i) == t.charAt(j)){
                    i++;j++;
                    count++;
                    if(count>max){
                        max = count;
                    }
                    while (s.charAt(i) == t.charAt(j)&&i<n&&j<m){
                        i++;j++;
                        count++;
                        if(count>max){
                            max = count;
                        }
                        if(i==n){
                            break;
                        }
                    }
                }
            }
        }
        System.out.println(max);
    }
}
